package com.biz.primus.ms.promotion.trans;

import com.biz.primus.model.promotion.vo.PurchaseGiftPromotionVo;
import com.biz.primus.model.promotion.vo.PurchaseGiftRuleVo;
import com.biz.primus.ms.promotion.dao.po.PurchaseGiftPromotion;
import com.google.common.collect.Lists;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 转换器 买赠满赠活动满赠 Po --> Vo
 *
 * @author david-liu
 */
public class PurchaseGiftPromotionPo2VoMapper implements Function<PurchaseGiftPromotion, PurchaseGiftPromotionVo> {
    @Override
    public PurchaseGiftPromotionVo apply(PurchaseGiftPromotion purchaseGiftPromotion) {
        PurchaseGiftPromotionVo vo = new PurchaseGiftPromotionVo();
        vo.setId(purchaseGiftPromotion.getId());
        vo.setName(vo.getName());
        vo.setDescription(vo.getDescription());
        vo.setChannels(purchaseGiftPromotion.getChannels());
        vo.setStartTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(purchaseGiftPromotion.getStartTime()));
        vo.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(purchaseGiftPromotion.getEndTime()));
        vo.setEffectMemberLevels(purchaseGiftPromotion.getEffectMemberLevelStringList());
        vo.setLimitType(purchaseGiftPromotion.getLimitType());
        vo.setGiftType(purchaseGiftPromotion.getGiftType());
        vo.setLimitQuantity(purchaseGiftPromotion.getLimitQuantity());
        vo.setLimitAmount(purchaseGiftPromotion.getLimitAmount());
        PurchaseGiftRuleVo giftRule = purchaseGiftPromotion.getPurchaseGiftRule();
        vo.setGiftProductQuantity(giftRule.getGiftProductQuantity());
        vo.setGiftProductIds(giftRule.getGiftProductIds());
        vo.setLimitGiftProductStock(giftRule.getLimitGiftProductStock());
        vo.setPointGiftType(giftRule.getPointGiftType());
        vo.setPointQuantity(giftRule.getPointQuantity());
        vo.setPointIncreaseTimes(giftRule.getPointIncreaseTimes());
        vo.setUserOnceGainLimit(purchaseGiftPromotion.getUserOnceGainLimit());
        return vo;
    }

    public List<PurchaseGiftPromotionVo> applyWithList(List<PurchaseGiftPromotion> purchaseGiftPromotions) {
        return Optional.ofNullable(purchaseGiftPromotions).orElse(Lists.newArrayList()).stream().map(this::apply).collect(Collectors.toList());
    }
}
